package neiltwist.cfd.akka;

import akka.actor.UntypedActor;

public class Worker extends UntypedActor {

	// define the work
	private float diffuse(float a, float x0, float x1, float x2, float x3, float x4) {
		// We need: 
		// a,
		// x0 i  j,
		// x1 = x i-1 j,
		// x2 = x i+1 j,
		// x3 = x i   j-1,
		// x4 = x i   j+1;
		
		// We don't need i or j if we have their values...
		// So we won't be passing around huge arrays...
		
		return ( x0 + a*( x1+x2+x3+x4 ) ) / (1+4*a);
	}

	// message handler
	public void onReceive(Object message) {
		if (message instanceof Work) {
			Work work = (Work) message;

			// perform the work
			float result = diffuse(work.getA(), work.getX0(), work.getX1(), work.getX2(), work.getX3(), work.getX4());
			// reply with the result
			getContext().reply(new Result(result, work.getI(),work.getJ()));
			
		} else if (message instanceof Finished) {
			getContext().reply(new Finished());
		} else throw new IllegalArgumentException("Unknown message [" + message + "]");
	}
}
